package com.gome.ocean.service.authority.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gome.ocean.common.enums.SmsValid;
import com.gome.ocean.common.utils.SmsUtils;
import com.gome.ocean.dao.authority.SmsDAO;
import com.gome.ocean.dao.model.authority.SmsBO;
import com.gome.ocean.service.authority.SmsService;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsDAO smsDao;

    @Override
    public boolean saveSmsBO(SmsBO smsBO) {
        smsBO.setSendTime(new Date());
        return smsDao.saveSmsBO(smsBO) > 0;
    }

    @Override
    public boolean deleteSmsBOById(Long id) {
        return smsDao.deleteSmsBOById(id) > 0;
    }

    @Override
    public boolean updateSmsBOById(SmsBO smsBO) {
        return smsDao.updateSmsBOById(smsBO) > 0;
    }

    @Override
    public boolean isValid(String phone, String code) {
        SmsBO smsBO = new SmsBO();
        smsBO.setPhone(phone);
        smsBO.setIsValid(Integer.valueOf(SmsValid.VALID.getValue()));
        smsBO = findSmsBOByPhone(smsBO);
        if (smsBO != null) {
            if (!smsBO.getCode().toLowerCase().equals(code.toLowerCase())) return false;
            smsBO.setIsValid(Integer.valueOf(SmsValid.NOT_VALID.getValue()));
            updateSmsBOById(smsBO);
            return true;
        }
        return false;
    }

    @Override
    public SmsBO findSmsBOByPhone(SmsBO smsBO) {
        return smsDao.findSmsBOByPhone(smsBO);
    }

    @Override
    public boolean sendSms(String phone, String code) {
        return SmsUtils.sendSms(phone, code);
    }

	@Override
	public boolean sendSms(String env, String phone, String code, String businessName, String templateId) {
		return SmsUtils.sendSms(env, phone, code, businessName, templateId);
	}
    
    
}
